/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.table;

import java.awt.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JTable;

import com.pearreview.application.Messages;

public class FindingListRenderer extends ListTableCellRenderer {

	private static final long serialVersionUID = 8765730185384796736L;
	
	public static final String[] IMAGE_STRINGS = {
			Messages.getString("PearReview.Scribe.findings.image"),
			Messages.getString("PearReview.Scribe.findings.images")};
	
	public static final String[] ASPECT_STRINGS = {
			Messages.getString("PearReview.Scribe.findings.aspect"),
			Messages.getString("PearReview.Scribe.findings.aspects")};
	
	public static final String[] REFERENCE_STRINGS = {
			Messages.getString("PearReview.Scribe.findings.reference"),
			Messages.getString("PearReview.Scribe.findings.references")};
	
	Map<Integer, ColumnType> columnMap = new HashMap<Integer, ColumnType>();
	
	public static enum ColumnType {
		References,
		Aspects,
		ExternalReferences
	}
	
	public FindingListRenderer() {
		columnMap.put(3, ColumnType.References);
		columnMap.put(4, ColumnType.Aspects);
		columnMap.put(5, ColumnType.ExternalReferences);
	}
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		
		super.getTableCellRendererComponent(table, 
				value, 
				isSelected, 
				hasFocus, 
				row, 
				column);
		
		int columnIndex = table.convertColumnIndexToModel(column);
		ColumnType columnType = columnMap.get(columnIndex);
		switch(columnType) {
		case Aspects:
			if(value == null) {
				setText("0 " + ASPECT_STRINGS[1]);
			} else if(value instanceof List<?>) {
				List<?> list = (List<?>) value;
				String suffix = list.size() == 1 ? 
						ASPECT_STRINGS[0] : ASPECT_STRINGS[1];
						
				setText(list.size() + " " + suffix);
			}
			break;
			
		case ExternalReferences:
			if(value == null) {
				setText("0 " + IMAGE_STRINGS[1]);
			} else if(value instanceof List<?>) {
				List<?> list = (List<?>) value;
				String suffix = list.size() == 1 ? 
						IMAGE_STRINGS[0] : IMAGE_STRINGS[1];
						
				setText(list.size() + " " + suffix);
			}
			break;
		}
		
		return this;
	}
	
	public void putColumnType(int columnIndex, ColumnType column) {
		columnMap.put(columnIndex, column);
	}
	
	public void removeColumnType(int columnIndex) {
		columnMap.remove(columnIndex);
	}
}
